#include<string>
#include<vector>
#include<iostream>
using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    void tree(TreeNode* root, int level, vector<vector<int>>& v)
    {
        if (root == nullptr)
        {
            return;
        }

        if (v.size() <= level)
        {
            vector<int> vv;
            vv.push_back(root->val);
            v.push_back(vv);
        }
        else
        {
            v[level].push_back(root->val);
        }
        tree(root->left, level + 1, v);
        tree(root->right, level + 1, v);
        return;
    }
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        tree(root, 0, ret);
        return ret;
    }
};